import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import type { Task } from '@/types'

export const useTasksStore = defineStore('tasks', () => {
  // State
  const tasks = ref<Task[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)

  // Getters
  const getTasksByStatus = computed(() => {
    return (status: string) => tasks.value.filter(task => task.status === status)
  })

  const getRunningTasks = computed(() => {
    return tasks.value.filter(task => task.status === 'running')
  })

  const totalTasks = computed(() => tasks.value.length)

  const completedTasks = computed(() => {
    return tasks.value.filter(task => task.status === 'completed').length
  })

  // Actions
  const fetchTasks = async() => {
    loading.value = true
    error.value = null
    try {
      // TODO: 实现获取任务列表的逻辑
      console.log('Fetching tasks...')
    } catch (err) {
      error.value = err instanceof Error ? err.message : 'Unknown error'
    } finally {
      loading.value = false
    }
  }

  const createTask = async(task: Omit<Task, 'id' | 'created_at' | 'updated_at'>) => {
    // TODO: 实现创建任务的逻辑
    console.log('Creating task:', task)
  }

  const updateTask = async(id: string, updates: Partial<Task>) => {
    // TODO: 实现更新任务的逻辑
    console.log('Updating task:', id, updates)
  }

  const deleteTask = async(id: string) => {
    // TODO: 实现删除任务的逻辑
    console.log('Deleting task:', id)
  }

  const startTask = async(id: string) => {
    // TODO: 实现启动任务的逻辑
    console.log('Starting task:', id)
  }

  const stopTask = async(id: string) => {
    // TODO: 实现停止任务的逻辑
    console.log('Stopping task:', id)
  }

  return {
    // State
    tasks,
    loading,
    error,

    // Getters
    getTasksByStatus,
    getRunningTasks,
    totalTasks,
    completedTasks,

    // Actions
    fetchTasks,
    createTask,
    updateTask,
    deleteTask,
    startTask,
    stopTask
  }
})